package net.zhuike.novel.app.ui.adapter;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import net.zhuike.novel.app.R;
import net.zhuike.novel.app.bean.BookChapterBean;
import net.zhuike.novel.app.bean.BookMixAToc;
import net.zhuike.novel.app.bean.user.MenuModel;
import net.zhuike.novel.app.ui.utils.BookManager;
import net.zhuike.novel.app.ui.utils.FileUtils;

import java.util.List;
import framework.common.easyadpter.abslistview.EasyLVAdapter;
import framework.common.easyadpter.abslistview.EasyLVHolder;

import static android.R.attr.value;

/**
 * @author wq.
 * @date 17/6/19.
 */
public class TocListAdapter extends EasyLVAdapter<BookChapterBean> {

    private int currentChapter;
    private String bookId;

    private boolean isEpub = false;

    public TocListAdapter(Context context, List<BookChapterBean> list, String bookId, int currentChapter) {
        super(context, list, R.layout.item_book_read_toc_list);
        this.currentChapter = currentChapter;
        this.bookId = bookId;
    }

    @Override
    public void convert(EasyLVHolder holder, int position, BookChapterBean chapters) {
        TextView tvTocItem = holder.getView(R.id.tvTocItem);
        LinearLayout showDownld=holder.getView(R.id.showDownld);
        tvTocItem.setText(chapters.getTitle());
        Drawable drawable;
        //TODO:目录显示设计的有点不好，需要靠成员变量是否为null来判断。
        //如果没有链接地址表示是本地文件
        if (chapters.getLink() == null){
            drawable = ContextCompat.getDrawable(mContext,R.mipmap.ic_toc_item_download);
            tvTocItem.setTextColor(ContextCompat.getColor(mContext, R.color.text_dark_color));
        }else{
            if (chapters.getBookId() != null
                    && BookManager
                    .isChapterCached(chapters.getBookId(),chapters.getTitle())){
                tvTocItem.setTextColor(ContextCompat.getColor(mContext, R.color.text_dark_color));
                drawable = ContextCompat.getDrawable(mContext,R.mipmap.ic_toc_item_download);
            }else{
                tvTocItem.setTextColor(ContextCompat.getColor(mContext, R.color.text_dark_color));
                drawable = ContextCompat.getDrawable(mContext, R.mipmap.ic_toc_item_normal);
                showDownld.setVisibility(View.GONE);
            }
        }
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        tvTocItem.setCompoundDrawables(drawable, null, null, null);
    }

    public void setCurrentChapter(int chapter) {
        currentChapter = chapter;
        notifyDataSetChanged();
    }

    public void setEpub(boolean isEpub) {
        this.isEpub = isEpub;
    }
}
